﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace NetOpen_System.Component
{
    /// <summary>
    /// Url匹配引擎
    /// </summary>
    public sealed class UrlMatchEngine
    {

        private SortedList<string, UrlMatchCondition> m_Conditions = new SortedList<string, UrlMatchCondition>();
        private string m_ErrorPage = "sorry.htm";
        private bool m_IsBlacklist = false;
        private string m_CitySiteList = "";

        public UrlMatchEngine(XmlNode node)
        {
            ParseFromNode(node);
        }

        private void ParseFromNode(XmlNode node)
        {
            if (node == null) return;
            XmlAttributeReader attReader = new XmlAttributeReader(node.Attributes);
            m_ErrorPage = attReader.GetString("ErrorPage", m_ErrorPage);
            m_IsBlacklist = attReader.GetBoolean("IsBlacklist", m_IsBlacklist);
            m_CitySiteList = attReader.GetString("CitySiteList", m_CitySiteList);

            if (!node.HasChildNodes) return;
            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.Name == "add")
                    AppendOrRemove(n, false);
                else if (n.Name == "remove")
                    AppendOrRemove(n, true);
                else if (n.Name == "clear")
                    m_Conditions.Clear();

            }

        }

        private void AppendOrRemove(XmlNode node, bool isRemove)
        {
            XmlAttributeReader attReader = new XmlAttributeReader(node.Attributes);
            string pattern = attReader.GetString("pattern", "");
            if (string.IsNullOrEmpty(pattern)) return;
            pattern = pattern.ToLower();
            if (isRemove)
            {
                if (m_Conditions.ContainsKey(pattern))
                    m_Conditions.Remove(pattern);
            }
            else
            {
                if (!m_Conditions.ContainsKey(pattern))
                    m_Conditions.Add(pattern, new UrlMatchCondition(pattern));
            }
        }

        /// <summary>
        /// 使用列表中的正则表达式匹配指定Url
        /// </summary>
        /// <param name="rawUrl">指定的Url</param>
        /// <returns>任一表达式匹配成功返回true，否则false</returns>
        public bool IsMatch(string rawUrl)
        {
            foreach (UrlMatchCondition condition in m_Conditions.Values)
            {
                if (condition.IsMatch(rawUrl))
                    return true;
            }
            return false;
        }
        /// <summary>
        /// 错误提示页
        /// </summary>
        public string ErrorPage
        {
            get { return m_ErrorPage; }
        }
        /// <summary>
        /// 是否黑名单
        /// </summary>
        public bool IsBlacklist
        {
            get { return m_IsBlacklist; }
        }

        /// <summary>
        /// 允许开放的城市
        /// </summary>
        public string CitySiteList
        {
            get { return m_CitySiteList; }
        }
    }
}
